home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Re: Q: '\n' character
- Date: 12 Apr 1996 00:16:12 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4kkvvsINN8fs@keats.ugrad.cs.ubc.ca>
- References: <4kj66f$k0o@ren.cei.net> <4kjh25INNf2e@anvil.ugrad.cs.ubc.ca> <4kko80$8np@news.ld.centuryinter.net>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4kko80$8np@news.ld.centuryinter.net>,
- Don & Gayle Peterson <dongayle@centuryinter.net> wrote:
- >It's a shame I didn't see the full original posting, but maybe your
- >solution is 'bullshit' too.
- >A long, long time ago, K&R taught me that the beauty in unix is
- >simplicity: don't re-invent a tool that already exists. If all the
- >original author wants to do is read 2 (or more) files side by side,
- >what's wrong with good old pr?
- >or, getting fancier, and comparing them, sdiff?...depending on the
- >situation, get nuts, use paste... join...
- >
- >Getting raw side-by-side output from text files at the shell is as
- >simple as:
- > pr -m file1 file2 ...
- >or
- > pr -t -m ....
- >if you don't want the pr header.
-
- I sometimes recommend unix commands myself---for example when someone wanted to
- know how to delete lines from a text stream, I facetiously suggested ``sed''.
-
- However, this is a C newsgroup, not comp.unix.shell. The C language is used in
- all kinds of places. It's somewhat arrogant to suggest UNIX solutions to C
- questions.
-
- >If this requirement is within C code where the multiple inputs
- >screw up the ability to use pipes, then forgive me for not knowing all
- >the facts.
- >But,
- >The other response using strlen isn't 'wrong'; there are lots of
- >ways to skin a cat.
-
- Not if you are being paid by the cat. :) And yes, the strlen() response was
- damn wrong. It completely ignored the possiblity that fgets() may return a
- string that doesn't have a newline at the end. Just the sort of useless answer
- that we don't need on comp.lang.c: incorrect, and is directed at the wrong
- question anyway.
-
- > have yet to use a version of Unix that will
- >blow up on forcing a null into a returned NULL pointer.
-
- What are you using for a Unix? Are you for real? Boy, you are going to get
- flamed for this.
-
- >For 18 years, I have coded scary stuff like
- > *strrchr(buffer, '\n') = '\0'
- >and have yet to have a problem. Try it on your system.
-
- I wouldn't write that on a resume.
-
- >Any problems?
-
- Are you serious? Can you say ``segmentation fault (core dumped)'' if the
- newline is not found? Dereferencing a null pointer is illegal in the C
- language, and is even caught by many environments that _don't_ have hardware
- memory mangement, e.g. the 16-bit compilers under MSDOS!
-
- >The mere nature of the author's request would indicate that
- >he is looking at text files, (what sense is there doing side-by-
- >sides of binaries?) So, why not a utility that works with, and
- >assumes, text? Unix text files have \n terminators. Correct me
- >if I'm wrong.
- >
- >So, why write a bunch of lines of _elegant_ code to simply
- >clobber a newline?
-
- Because it's less typing than the inelegant (and incorrect, unrobust)
- equivalent?
-
- >I also notice that your solution does not take into account any
- >output formatting: what happens to the output from f2 when f1 is
- >shorter that f2? From what I see, it starts looking like output from
- >f1.
-
- This was not called for in the informal requirements. In fact, if f1 runs out
- of lines, f2 is flushed left. The original posting (which I'm aware you didn't
- see) had not specified any sort of separator character, though adding one is
- trivial.
-
- >Is your version really faster?
-
- Maybe not measurably, but I can't imagine any implementation where avoiding
- needless buffering and scanning would not save machine cycles. Not that saving
- machine cycles is the primary motivation here.
-
- >Does it really matter for the author's
- >needs? Is it easier to follow? Is it smaller? Easier to debug when
-
- Yes.
-
- >buried in a few thousand lines of code? By the way, isn't the stdio
-
- Undoubtedly.
-
- >library's intent to create more efficient i/o? Is getc/putchar really
- >more efficient than fgets or fread?
-
- In certain contexts, yes. It's not that they are _inherently_ inefficient. It's
- improper use that makes approach that makes them inefficient. For instance, it
- doesn't always make sense to fgets() something and then scan the results! Why
- not just scan the standard IO stream? The macros directly access the buffer
- associated with the FILE structure.
-
- >I'm new to this group, and your type of response is kind of scary:
- >does everyone here have a chip on their shoulder?
- >Cut others a little slack!
-
- Incorrect answers, or answers to the wrong question are not helpful to anyone.
- What is scary is that the proliferation of poor answers continues once the
- question has already been beat to death and has been correctly answered by
- people who actually _test_ their code, however trivial, before posting it to
- the newsgroup.
- --
-
-